package cn.stylefeng.guns.modular.gridsystem.modbus;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

@Data
public class ModbusDeviceStatus {

    private int offset = 10;
    private short status;

    public void setGatewayStatus(boolean[] gatewayStatus) {

        for (int i = 0; i < gatewayStatus.length; i++) {
            boolean mStatus = gatewayStatus[i];
            status = (short) (((status) | ((mStatus ? 1 : 0)<<i)) & 0xffff);
        }
    }

    public List<Short> encode() {

        //short status;
        List<Short> data = new ArrayList<>();
        data.add(status);

        return data;
    }

    @Override
    public String toString() {
        return "ModbusDeviceStatus{" +
                "offset=" + offset +
                ", status=" + status +
                '}';
    }

    /*public static void main(String[] args) {
        ModbusDeviceStatus log = new ModbusDeviceStatus();
        log.setGatewayStatus(new boolean[]{false,true,true,true,true,true,true,true});
        System.out.println(log);

        final List<Short> encode = log.encode();

        System.out.println(Integer.toBinaryString(Integer.valueOf(encode.get(0))));

        ModbusDeviceStatus logDecode = ModbusDeviceStatus.decode(encode);
        System.out.println(logDecode);
    }*/

    public static ModbusDeviceStatus decode(List<Short> data) {
        ModbusDeviceStatus log = new ModbusDeviceStatus();
        log.setStatus(data.get(0));
        return log;
    }
}
